package function;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.google.common.collect.Lists;

import common.AntiCrack;
import common.MiddleCommonUtils;
import dataType.WindowsClient;
import form.CommonForm;
import form.WindowsMsg;
import function.W32API.HWND;

public class GetFouce {

	public HWND havClient(CommonForm cf, String name, String id) {
		try {
			W32API.HWND hwnd;
			String ClientName = "梦幻西游ONLINE - (" + cf.getArea() + "["
					+ cf.getServer() + "] - " + name + "[" + id + "])";
			hwnd = User32.INSTANCE.FindWindow(0, ClientName);
			// hwnd = User32.INSTANCE.FindWindow(0, "1.txt - メモ帳");
			ClientName = null;
			return hwnd;
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

//	梦幻西游 ONLINE - (笑傲三界[华山论剑] - 柯Ⅱ初丹ㄋ[681954])
	public static List<WindowsClient> mhClients(){
		List<WindowsClient> clients = Lists.newArrayList();
		W32API.HWND hwnd = null;
		while(true){
			byte[] text = new byte[1024];
			hwnd = User32.INSTANCE.FindWindowEx(null, hwnd, 0, 0);
			if(hwnd == null){
				break;
			}
			int length = User32.INSTANCE.GetWindowTextA(hwnd, text, 1024);
			try {
				String title = new String(text,0,length,"gb2312");
				if(title.indexOf("梦幻西游 ONLINE")!=-1){
					WindowsClient client = new WindowsClient();
					client.setHwnd(hwnd);
					client.setTitle(title);
					client.setShowName(MiddleCommonUtils.getName(title));
					client.setId(MiddleCommonUtils.getId(title));
					clients.add(client);
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return clients;
		
	}
	
	public void getForce(WindowsClient client){
		User32.INSTANCE.SetForegroundWindow(client.getHwnd());
	}
	
	public boolean getForce(CommonForm cf, WindowsMsg win) {
		try {
				User32.INSTANCE.ShowWindow(win.getHwnd(), 9);
				User32.INSTANCE.SetForegroundWindow(win.getHwnd());
				return true;
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}
}
